import React from 'react';
import { Button, Card, Form, Input, InputNumber, Space, Spin} from "antd";
import {Link, useParams} from "react-router-dom";
import LogFactory from "log/LogFactory";
import {Part} from "type/Part";
import TextArea from "antd/es/input/TextArea";
import useFormUpdate from "hooks/UseFormUpdate";
import {MATHEMATICAL_PRECISION} from "constant/Common";

import {fetchUpdatePart} from "../../../redux/reducer/async/partAsyncAction";
import PartProviderSelect from "./PartProviderSelect";
import PartGroupSelect from "./PartGroupSelect";
import {requestPartById} from "../RequestHandler";

const log = LogFactory.getLog("PartUpdateComponent");

const formConvert = (data: Partial<Part>) => {
    data.salePrice && (data.salePrice = data.salePrice / MATHEMATICAL_PRECISION);
    data.costPrice && (data.costPrice = data.costPrice / MATHEMATICAL_PRECISION) ;
    return data ;
};
const dataConvert = (data: Partial<Part>) => {
    data.salePrice && (data.salePrice = data.salePrice * MATHEMATICAL_PRECISION);
    data.costPrice && (data.costPrice = data.costPrice * MATHEMATICAL_PRECISION) ;
    return data ;
} ;
const PartUpdateComponent = () => {
    const id = useParams().id ;
    const [form] = Form.useForm<Part>();
    const [loading,callback] = useFormUpdate<Part>(requestPartById(id),form,fetchUpdatePart,dataConvert,formConvert) ;

    return (
        <Card title={"更新零件信息"} extra={<Link to={".."}>零件列表</Link>}>
          <Spin spinning={loading}>
            <Form<Part>
                layout={"horizontal"}
                form={form}
                labelCol={{span: 5}}
                wrapperCol={{span: 10}}
                onFinish={callback}
            >
                <Form.Item<Part> name={"partName"} label="零件名称"
                                 rules={[{required: true, message: '此字段必须输入'}]}>
                    <Input placeholder={"请输入零件名称"}/>
                </Form.Item>
                <Form.Item<Part> name={"partDesc"} label="零件描述">
                    <Input placeholder={"请输入零件描述信息"}/>
                </Form.Item>
                <Form.Item<Part> name={"figureNumber"} label="零件图号">
                    <Input placeholder={"请输入零件图号"}/>
                </Form.Item>
                <Form.Item<Part> name={"partSize"} label="零件尺寸">
                    <Input placeholder={"请输入零件尺寸"}/>
                </Form.Item>
                <PartProviderSelect form={form} disableValue={true}/>
                <PartGroupSelect form={form} disableValue={true}/>
                <Form.Item<Part> name={"repertorySize"} label="库存数量">
                    <InputNumber min={0} addonAfter={"个"}/>
                </Form.Item>
                <Form.Item<Part> name={"costPrice"} label="进货价格">
                    <InputNumber min={0.00} precision={2} addonAfter={"¥"}/>
                </Form.Item>
                <Form.Item<Part> name={"salePrice"} label="销售价格">
                    <InputNumber min={0.00} precision={2} addonAfter={"¥"}/>
                </Form.Item>
                <Form.Item<Part> name="order" label="数据排序">
                    <InputNumber min={0} addonAfter={"顺序"}/>
                </Form.Item>
                <Form.Item label="备注信息" name={"remark"}>
                    <TextArea rows={3} />
                </Form.Item>
                <Form.Item wrapperCol={{offset: 6,span: 16}}>
                    <Space>
                        <Button loading={loading} type="primary" htmlType="submit">提交修改</Button>
                        <Button type="link" htmlType="button">
                            填充字段
                        </Button>
                    </Space>
                </Form.Item>
            </Form>
        </Spin>
        </Card>
    );
};

export default PartUpdateComponent;